﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZkSurvey.Common.SurveyMgr;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace ZkSurvey.DAL.SurveyMgr
{
    public class QuestionDa
    {
        public static void InsertQuestion(Question question)
        {
            InsertQuestion(question, DBHelper.GetDatabase(), null);
        }
        public static void InsertQuestion(Question question, TranscationHelper trans)
        {
            InsertQuestion(question, trans.DataBase, trans.Transaction);
        }
        private static void InsertQuestion(Question question, Database db, DbTransaction trans)
        {
            object[] parameters = new object[]
            {
                question.SurveyId,
                question.Title,
                (int)question.Type,
                question.AllowInput,
                question.Sort
            };

            try
            {
                if (trans == null)
                    question.Id = Convert.ToInt32(db.ExecuteScalar("Usp_InsertQuestion", parameters));
                else
                    question.Id = Convert.ToInt32(db.ExecuteScalar(trans, "Usp_InsertQuestion", parameters));
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static void DeleteQuestion(int id)
        {
            DeleteQuestion(id, DBHelper.GetDatabase(), null);
        }
        public static void DeleteQuestion(int id, TranscationHelper trans)
        {
            DeleteQuestion(id, trans.DataBase, trans.Transaction);
        }
        private static void DeleteQuestion(int id, Database db, DbTransaction trans)
        {
            try
            {
                if (trans == null)
                    db.ExecuteNonQuery("Usp_DeleteQuestion", id);
                else
                    db.ExecuteNonQuery(trans, "Usp_DeleteQuestion", id);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static void UpdateQuestion(Question question)
        {
            UpdateQuestion(question, DBHelper.GetDatabase(), null);
        }
        public static void UpdateQuestion(Question question, TranscationHelper trans)
        {
            UpdateQuestion(question, trans.DataBase, trans.Transaction);
        }
        private static void UpdateQuestion(Question question, Database db, DbTransaction trans)
        {
            object[] parameters = new object[]
            {
                question.Id,
                question.SurveyId,
                question.Title,
               (int)question.Type,
                question.AllowInput,
                question.Sort
            };

            try
            {
                if (trans == null)
                     db.ExecuteNonQuery("Usp_UpdateQuestion", parameters);
                else
                    db.ExecuteNonQuery(trans, "Usp_UpdateQuestion", parameters);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static Question GetQuestion(int id)
        {
            Database db = DBHelper.GetDatabase();
            Question question = null;

            using (IDataReader dataReader = db.ExecuteReader("Usp_GetQuestion", id))
            {
                if (dataReader.Read())
                {
                    question = PopulateQuestion(dataReader);
                }
            }
            return question;
        }

        public static List<Question> GetQuestionList(int surveyId)
        {
            return GetQuestionList(surveyId, DBHelper.GetDatabase(), null);
        }
        public static List<Question> GetQuestionList(int surveyId, TranscationHelper trans)
        {
            return GetQuestionList(surveyId, trans.DataBase, trans.Transaction);
        }
        private static List<Question> GetQuestionList(int surveyId, Database db, DbTransaction trans)
        {
            List<Question> questions = new List<Question>();
            try
            {
                IDataReader dataReader;
                if (trans == null)
                    dataReader = db.ExecuteReader("Usp_GetQuestionList", surveyId);
                else
                    dataReader = db.ExecuteReader(trans, "Usp_GetQuestionList", surveyId);

                using (dataReader)
                {
                    while (dataReader.Read())
                    {
                        questions.Add(PopulateQuestion(dataReader));
                    }
                }

            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
            return questions;
        }

        private static Question PopulateQuestion(IDataReader dataReader)
        {
            Question question = new Question();
            question.Id = dataReader.GetInt32(0);
            question.SurveyId = dataReader.GetInt32(1);
            question.Title = dataReader.GetString(2);
            question.Type = (QuestionType)dataReader.GetInt32(3);
            question.AllowInput = dataReader.GetBoolean(4);
            question.Sort = dataReader.GetInt32(5);

            return question;
        }
    }
}
